<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Login Page</title>
    <!-- 样 式 文 件 -->
    <link rel="stylesheet" href="component/pear/css/pear.css"/>
    <link rel="stylesheet" href="admin/css/other/login.css"/>
</head>
<!-- 代 码 结 构 -->
<body background="admin/images/background.svg" style="background-size: cover;">
<form class="layui-form" action="javascript:void(0);" lay-filter="lms_form">
    <div class="layui-form-item">
        <img class="logo" src="admin/images/logo.png"/>
        <div class="title">Pear Admin</div>
        <div class="desc"></div>
    </div>
    <div class="layui-form-item">
        <input name="lms_account" placeholder="账 户 : " required lay-verify="required" hover class="layui-input" value="admin"/>
    </div>
    <div class="layui-form-item">
        <input name="lms_pass" type="password" autocomplete="off" required placeholder="密 码 : " lay-verify="required" hover class="layui-input" value="123456"/>
    </div>
    <div class="layui-form-item">
        <input name="lms_captcha" placeholder="验证码 : " hover lay-verify="required" class="code layui-input"/>
        <img id="imgCaptcha" src="" class="codeImage" lay-filter="imgCaptcha"/>
    </div>
    <div class="layui-form-item">
         <input type="checkbox" name="" title="记住密码" lay-skin="primary" checked>
    </div>
    <div class="layui-form-item">
        <button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
            登 入
        </button>
    </div>
</form>
<!-- 资 源 引 入 -->
<script src="component/layui/layui.js"></script>
<script src="component/pear/pear.js"></script>
<script src="lms/base64.js"></script>
<!-- <script src="lms/md5.js"></script> -->
<script src="lms/common.js"></script>
<!--<script src="lms/config.js"></script>-->
<script>

    if (checkLogin()) {
        location.href = lms_config.base_path + "index.html";
    }

    const LOGIN_URL = lms_config.api_url + "/api/v1/Login/login"
    const CAPTCHA_URL = lms_config.api_url + "/api/v1/Login/createCaptcha"

    layui.use(['form', 'button', 'popup', 'jquery', 'http', 'encrypt'], function () {
        var form = layui.form;
        var button = layui.button;
        var popup = layui.popup;
        var $ = layui.jquery;
        var encrypt = layui.encrypt;

        const http = layui.http;


        // 登 录 提 交
        form.on('submit(login)', function (data) {
            $.ajax({
                url: LOGIN_URL, // 请求路径
                type: "POST", //请求方式
                contentType: 'application/x-www-form-urlencoded',
                data: {
                    "account": data.field.lms_account,
                    "password": encrypt.md5(data.field.lms_pass),
                    captcha: data.field.lms_captcha,
                    cid: layui.sessionData("lms").captchaKey,
                },
                dataType: 'json',	//预期服务器返回的数据类型；设置接受到的响应数据的格式

                success: function (ret) {

                    if (ret.code !== 0) {
                        popup.failure(ret.msg, function () {
                            refreshCaptcha();
                        });
                        return;
                    }

                    setUserInfo({
                        isLogin: true,
                        "token": ret.data.token,
                        info: ret.data.info,
                        rules: ret.data.rules,
                    });

                    /// 动画
                    button.load({
                        elem: '.login',
                        time: 1500,
                        done: function () {
                            popup.success("登录成功", function () {
                                location.href = lms_config.base_path + "index.html"
                            });
                        }
                    });


                }, //响应成功后的回调函数
                error: function (ret) {
                    console.error(ret);
                    alert("出错" + ret.status + "：" + ret.responseText);
                }, //表示如果请求响应出现错误，会执行的回调函数

            });


            return false;
        });


        //点击验证码
        $("#imgCaptcha").click(function () {
            refreshCaptcha();
        });


        //刷新验证码
        refreshCaptcha();

        function refreshCaptcha() {
            $.ajax({
                url: CAPTCHA_URL, //请求路径
                type: "GET", //请求方式
                contentType: 'application/x-www-form-urlencoded',
                data: "", //请求参数
                dataType: 'json',	//预期服务器返回的数据类型；设置接受到的响应数据的格式
                success: function (data) {
                    // $("#imgCaptcha").attr("src", 'data:image/png;base64,' + data.img);
                    $("#imgCaptcha").attr("src", data.img);
                    layui.sessionData("lms", {
                        key: "captchaKey",
                        value: data.key
                    });
                }, //响应成功后的回调函数
                error: function (data) {
                    console.log(data);
                    //alert("出错啦...")
                }, //表示如果请求响应出现错误，会执行的回调函数

            });
        }
    })

    layui.$(document).ready(function () {
        if (window != top) {
            top.location.href = location.href;
        }
    });
</script>
</body>
</html>
